package com.example.dingzexi_homework1;

import android.widget.Button;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
public class MyFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_my, container, false);

        // 找到加好友按钮
        Button buttonAddFriend = view.findViewById(R.id.buttonAddFriend);

        // 设置点击事件监听器
        buttonAddFriend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 替换为 AddFriendFragment
                replaceFragment(new AddFriendFragment());
            }
        });

        return view;
    }

    // 替换 Fragment 的方法
    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getChildFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container, fragment); // 确保这里的容器ID与你的布局文件中的ID相匹配
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
}